package com.tensquare.user.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.tensquare.user.pojo.User;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户数据访问接口
 * @author Administrator
 *
 */
public interface UserDao extends JpaRepository<User,String>,JpaSpecificationExecutor<User>{
    /**
     * 根据手机号查询用户
     * @param mobile
     * @return
     */
    public User findByMobile(String mobile);

    //根据手机号和密码查询用户
    public User findUserByMobileAndPassword(String mobile,String password);

    //更新关注数
    @Modifying
    @Query(value = "update tb_user set followcount=followcount+1 where id=?",nativeQuery = true)
    public void updateFollowcount(String id);
    //更新粉丝数
    @Modifying
    @Query(value = "update tb_user set fanscount=fanscount+1 where id=?",nativeQuery = true)
    public void updateFanscount(String userId);
    //更新中间表-tb_follow表
    @Modifying
    @Query(value = "insert into tb_follow values(?,?)",nativeQuery = true)
    public void updaTbFollow(String id,String userId);

    //删除关注数
    @Modifying
    @Query(value = "update tb_user set followcount=followcount-1 where id=? and followcount>0",nativeQuery = true)
    public void deleteFollowCount(String id);
    //删除粉丝数
    @Modifying
    @Query(value = "update tb_user set fanscount=fanscount-1 where id=? and fanscount>0",nativeQuery = true)
    public void deleteFancount(String userId);
    //删除中间表-tb_follow的关联
    @Modifying
    @Query(value = "delete from tb_follow where targetuser=? and userid=?",nativeQuery = true)
    public void deleteTbFollow(String id,String userId);

    //根据id查询我的粉丝列表并分页
    @Query(value = "select * from tb_user where id in (select targetuser from tb_follow where userid=?1) ORDER BY ?#{#pageable}",
            countQuery = "select COUNT(*) from tb_follow where userid=?1",nativeQuery = true)
    public Page<User> findMyfans(String id, Pageable pageable);
    //根据id查询我的关注列表并分页
    @Query(value = "select * from tb_user where id in (select userid from tb_follow where targetuser =?1) ORDER BY ?#{#pageable}",
            countQuery = "select COUNT(*) from tb_follow where targetuser=?1",nativeQuery = true)
    public Page<User> findMyfollow(String id, Pageable pageable);
    //根据id查询我得关注列表id
    @Query(value = "select userid from tb_follow where targetuser=?",nativeQuery = true)
    public List<String> findMyfollowid(String id);

    /**
     * 更新粉丝数
     * @param userid 用户ID
     * @param x 粉丝数
     */
    @Modifying
    @Query("update User u set u.fanscount=u.fanscount+?2 where u.id=?1")
    public void incFanscount(String userid,int x);
    /**
     * 更新关注数
     * @param userid 用户ID
     * @param x 粉丝数
     */
    @Modifying
    @Query("update User u set u.followcount=u.followcount+?2 where u.id=?1")
            public void incFollowcount(String userid,int x);

    public int findByNickname(String nickname);

    //设置用户头像
    @Modifying
    @Query(value = "update tb_user set avatar=?2 where id=?1",nativeQuery = true)
    public void setAvator(String uid,String avatar);
}
